package com.fowo.api.controller.handleWork;

import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.model.listing.father.ListingBatchPairBo;
import com.fowo.api.model.listing.father.ListingFatherVo;
import com.fowo.api.model.listing.father.ListingPairBo;
import com.fowo.api.model.listing.father.ListingPrincipalBo;
import com.fowo.api.service.handleWork.ListingDService;
import com.fowo.api.sys.entity.SysFile;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/api/listing-father")
public class ListingDController {

    @Resource
    private ListingDService listingDService;

    @Operation(summary = "配对/批量配对")
    @PostMapping("/pairProduct")
    public R pair(@RequestBody ListingBatchPairBo param){
        return R.run(()->listingDService.pair(param));
    }

    @Operation(summary = "导入配对")
    @PostMapping("/uploadPair")
    public R uploadPair(@RequestBody SysFile sysFile){
        return R.run(()->listingDService.uploadPair(sysFile));
    }

    @Operation(summary = "设置负责人")
    @PostMapping("/principal")
    public R updatePrincipal(@RequestBody ListingPrincipalBo param){
        return R.run(()->listingDService.updatePrincipal(param));
    }

    @Operation(summary = "导入设置负责人")
    @PostMapping("/uploadPrincipal")
    public R uploadPrincipal(@RequestBody SysFile sysFile){
        return R.run(()->listingDService.uploadPrincipal(sysFile));
    }

    @Operation(summary = "编辑本地信息")
    @PostMapping("/updateLocal")
    public R updateLocal(@RequestBody ListingFatherVo model){
        return R.run(()->listingDService.updateLocal(model));
    }

    @Operation(summary = "同步店铺Listing")
    @GetMapping("/sync/{shopId}")
    public R syncListingByShop(@PathVariable("shopId") Long shopId){
        return R.run(() -> listingDService.syncListingByShop(shopId));
    }

    @Operation(summary = "全部同步Listing")
    @GetMapping("/sync")
    public R syncListing(){
        return R.run(() -> listingDService.syncListing());
    }

    @Operation(summary = "下载导入负责人模版")
    @GetMapping("import/template/principal")
    public void downloadImportTemplatePrincipal(HttpServletResponse response)
            throws IOException {
        ResponseUtils.setAttachmentFileName(response, "Listing导入负责人模版.xlsx");
        InputStream inputStream = new ClassPathResource(
                "templates/ListingFatherPrincipal.xlsx"
        )
                .getInputStream();
        StreamUtils.copy(inputStream, response.getOutputStream());
    }

    @Operation(summary = "下载配对导入模版")
    @GetMapping("import/template/pair")
    public void downloadImportTemplatePair(HttpServletResponse response)
            throws IOException {
        ResponseUtils.setAttachmentFileName(response, "Listing导入配对模版.xlsx");
        InputStream inputStream = new ClassPathResource(
                "templates/ListingFatherPair.xlsx"
        )
                .getInputStream();
        StreamUtils.copy(inputStream, response.getOutputStream());
    }
}
